using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using T2.WebSecurity.Business;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity.Search;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.Business.Entities;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.Business.Entities.Search;
using T2.WebSecurity.ASPMVC.ViewModel;
using System.Web.Http;
using System.Net.Http;
using System.Net;

namespace T2.WebSecurity.ASPMVC.Controllers
{
    [System.Web.Http.Authorize]
    public partial class ApplicationRolesAPIController : ApiController
    {
         
        public HttpResponseMessage PostApplicationRoles(ApplicationRoles_ui param)
        {

            try
            {
                ApplicationRolesViewModel model = new ApplicationRolesViewModel(false);
                string[] roles = System.Web.Security.Roles.GetRolesForUser();
                param.Role = roles.Length > 0 ? roles[0] : "system";
                if (param.Action.Equals(Common.MyEnum.Actions.Update.ToString()))
                {
                    param.ModifiedBy = User.Identity.Name;
                    param.ModifiedDt = DateTime.Now;
                }
                else
                {
                    param.CreatedBy = User.Identity.Name;
                    param.CreatedDt = DateTime.Now;
                    param.ModifiedBy = User.Identity.Name;
                    param.ModifiedDt = DateTime.Now;
                }

                Int32 retVal = model.Set(param);
                if (retVal == 1)
                {
                    HttpResponseMessage message = new HttpResponseMessage();
                    message.ReasonPhrase = "Completed saving ApplicationRoles";
                    message.StatusCode = HttpStatusCode.OK;
                    return message;
                }
                else
                {
                    HttpResponseMessage message = new HttpResponseMessage();
                    message.ReasonPhrase = "ApplicationRoles did not save";
                    message.StatusCode = HttpStatusCode.NotAcceptable;
                    return message;
                }
            }
            catch (Exception ex)
            {
                HttpResponseMessage message = new HttpResponseMessage();
                message.ReasonPhrase = ex.Message;
                message.StatusCode = HttpStatusCode.InternalServerError;
                return message;
            }
        }

        
        public List<IApplicationRoles_ui> GetApplicationRoles(string Action, string Caller, string SortColumn, string SortType, string Role, int PageNumber, int PageSize)
        {
            
            try
            {
                IApplicationRoles_ui param = new ApplicationRoles_ui();
                param.Action = Action;
                param.Caller = Caller;
                param.SortColumn = SortColumn;
                param.SortType = SortType;
                param.Role = Role;
                param.PageNumber = PageNumber;
                param.PageSize = PageSize;
                //if (Session["ApplicationId"] == null)
                //{
                //    throw new Exception("Application is not selected");
                //}
                //param.ApplicationID = Convert.ToInt32(HttpContext.Current.Session["ApplicationId"]);
                ApplicationRolesViewModel model = new ApplicationRolesViewModel(false);
                List<IApplicationRoles_ui> retVal = model.ModelList(param);
                return retVal;
            }
            catch (Exception ex)
            {
                HttpResponseMessage message = new HttpResponseMessage();
                message.ReasonPhrase = ex.Message;
                message.StatusCode = HttpStatusCode.InternalServerError;
                throw new HttpResponseException(message);  
            }
        }

        
		public List<IApplicationRoles_ui> GetApplicationRolesSearch(ApplicationRolesSearch_ui param)
        {
            
            try
            {
                ApplicationRolesViewModel model = new ApplicationRolesViewModel(false);
                List<IApplicationRoles_ui> retVal = model.SearchList(param);
                return retVal;
            }
            catch (Exception ex)
            {
                HttpResponseMessage message = new HttpResponseMessage();
                message.ReasonPhrase = ex.Message;
                message.StatusCode = HttpStatusCode.InternalServerError;
                throw new HttpResponseException(message);  
            }
        }	


    }
}